3 * Oracle-specific installer.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use Wikimedia\Rdbms\DBConnectionError
;
27 * Class for setting up the MediaWiki database using Oracle.
32 class OracleInstaller
extends DatabaseInstaller
{
34 protected $globalNames = [
42 protected $internalDefaults = [
43 '_OracleDefTS' => 'USERS',
44 '_OracleTempTS' => 'TEMP',
45 '_InstallUser' => 'SYSTEM',
48 public $minimumVersion = '9.0.1'; // 9iR1
50 protected $connError = null;
52 public function getName() {
56 public function isCompiled() {
57 return self
::checkExtension( 'oci8' );
60 public function getConnectForm() {
61 if ( $this->getVar( 'wgDBserver' ) == 'localhost' ) {
62 $this->parent
->setVar( 'wgDBserver', '' );
65 return $this->getTextBox(
67 'config-db-host-oracle',
69 $this->parent
->getHelpBox( 'config-db-host-oracle-help' )
71 Html
::openElement( 'fieldset' ) .
72 Html
::element( 'legend', [], wfMessage( 'config-db-wiki-settings' )->text() ) .
73 $this->getTextBox( 'wgDBprefix', 'config-db-prefix' ) .
74 $this->getTextBox( '_OracleDefTS', 'config-oracle-def-ts' ) .
77 'config-oracle-temp-ts',
79 $this->parent
->getHelpBox( 'config-db-oracle-help' )
81 Html
::closeElement( 'fieldset' ) .
82 $this->parent
->getWarningBox( wfMessage( 'config-db-account-oracle-warn' )->text() ) .
83 $this->getInstallUserBox() .
84 $this->getWebUserBox();
87 public function submitInstallUserBox() {
88 parent
::submitInstallUserBox();
89 $this->parent
->setVar( '_InstallDBname', $this->getVar( '_InstallUser' ) );
91 return Status
::newGood();
94 public function submitConnectForm() {
95 // Get variables from the request
96 $newValues = $this->setVarsFromRequest( [
102 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
105 $status = Status
::newGood();
106 if ( !strlen( $newValues['wgDBserver'] ) ) {
107 $status->fatal( 'config-missing-db-server-oracle' );
108 } elseif ( !self
::checkConnectStringFormat( $newValues['wgDBserver'] ) ) {
109 $status->fatal( 'config-invalid-db-server-oracle', $newValues['wgDBserver'] );
111 if ( !preg_match( '/^[a-zA-Z0-9_]*$/', $newValues['wgDBprefix'] ) ) {
112 $status->fatal( 'config-invalid-schema', $newValues['wgDBprefix'] );
114 if ( !$status->isOK() ) {
119 $status = $this->submitInstallUserBox();
120 if ( !$status->isOK() ) {
124 // Try to connect trough multiple scenarios
125 // Scenario 1: Install with a manually created account
126 $status = $this->getConnection();
127 if ( !$status->isOK() ) {
128 if ( $this->connError
== 28009 ) {
129 // _InstallUser seems to be a SYSDBA
130 // Scenario 2: Create user with SYSDBA and install with new user
131 $status = $this->submitWebUserBox();
132 if ( !$status->isOK() ) {
135 $status = $this->openSYSDBAConnection();
136 if ( !$status->isOK() ) {
139 if ( !$this->getVar( '_CreateDBAccount' ) ) {
140 $status->fatal( 'config-db-sys-create-oracle' );
146 // check for web user credentials
147 // Scenario 3: Install with a priviliged user but use a restricted user
148 $statusIS3 = $this->submitWebUserBox();
149 if ( !$statusIS3->isOK() ) {
155 * @var $conn Database
157 $conn = $status->value
;
160 $version = $conn->getServerVersion();
161 if ( version_compare( $version, $this->minimumVersion
) < 0 ) {
162 return Status
::newFatal( 'config-oracle-old', $this->minimumVersion
, $version );
168 public function openConnection() {
169 $status = Status
::newGood();
171 $db = new DatabaseOracle(
172 $this->getVar( 'wgDBserver' ),
173 $this->getVar( '_InstallUser' ),
174 $this->getVar( '_InstallPassword' ),
175 $this->getVar( '_InstallDBname' ),
177 $this->getVar( 'wgDBprefix' )
179 $status->value
= $db;
180 } catch ( DBConnectionError
$e ) {
181 $this->connError
= $e->db
->lastErrno();
182 $status->fatal( 'config-connection-error', $e->getMessage() );
188 public function openSYSDBAConnection() {
189 $status = Status
::newGood();
191 $db = new DatabaseOracle(
192 $this->getVar( 'wgDBserver' ),
193 $this->getVar( '_InstallUser' ),
194 $this->getVar( '_InstallPassword' ),
195 $this->getVar( '_InstallDBname' ),
197 $this->getVar( 'wgDBprefix' )
199 $status->value
= $db;
200 } catch ( DBConnectionError
$e ) {
201 $this->connError
= $e->db
->lastErrno();
202 $status->fatal( 'config-connection-error', $e->getMessage() );
208 public function needsUpgrade() {
209 $tempDBname = $this->getVar( 'wgDBname' );
210 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
211 $retVal = parent
::needsUpgrade();
212 $this->parent
->setVar( 'wgDBname', $tempDBname );
217 public function preInstall() {
218 # Add our user callback to installSteps, right before the tables are created.
221 'callback' => [ $this, 'setupUser' ]
223 $this->parent
->addInstallStep( $callback, 'database' );
226 public function setupDatabase() {
227 $status = Status
::newGood();
232 public function setupUser() {
235 if ( !$this->getVar( '_CreateDBAccount' ) ) {
236 return Status
::newGood();
239 // normaly only SYSDBA users can create accounts
240 $status = $this->openSYSDBAConnection();
241 if ( !$status->isOK() ) {
242 if ( $this->connError
== 1031 ) {
243 // insufficient privileges (looks like a normal user)
244 $status = $this->openConnection();
245 if ( !$status->isOK() ) {
253 $this->db
= $status->value
;
254 $this->setupSchemaVars();
256 if ( !$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
257 $this->db
->setFlag( DBO_DDLMODE
);
258 $error = $this->db
->sourceFile( "$IP/maintenance/oracle/user.sql" );
259 if ( $error !== true ||
!$this->db
->selectDB( $this->getVar( 'wgDBuser' ) ) ) {
260 $status->fatal( 'config-install-user-failed', $this->getVar( 'wgDBuser' ), $error );
262 } elseif ( $this->db
->getFlag( DBO_SYSDBA
) ) {
263 $status->fatal( 'config-db-sys-user-exists-oracle', $this->getVar( 'wgDBuser' ) );
266 if ( $status->isOK() ) {
267 // user created or already existing, switching back to a normal connection
268 // as the new user has all needed privileges to setup the rest of the schema
269 // i will be using that user as _InstallUser from this point on
272 $this->parent
->setVar( '_InstallUser', $this->getVar( 'wgDBuser' ) );
273 $this->parent
->setVar( '_InstallPassword', $this->getVar( 'wgDBpassword' ) );
274 $this->parent
->setVar( '_InstallDBname', $this->getVar( 'wgDBuser' ) );
275 $status = $this->getConnection();
282 * Overload: after this action field info table has to be rebuilt
285 public function createTables() {
286 $this->setupSchemaVars();
287 $this->db
->setFlag( DBO_DDLMODE
);
288 $this->parent
->setVar( 'wgDBname', $this->getVar( 'wgDBuser' ) );
289 $status = parent
::createTables();
290 $this->db
->clearFlag( DBO_DDLMODE
);
292 $this->db
->query( 'BEGIN fill_wiki_info; END;' );
297 public function getSchemaVars() {
299 # These variables are used by maintenance/oracle/user.sql
305 # These are used by tables.sql
309 foreach ( $varNames as $name ) {
310 $vars[$name] = $this->getVar( $name );
316 public function getLocalSettings() {
317 $prefix = $this->getVar( 'wgDBprefix' );
319 return "# Oracle specific settings
320 \$wgDBprefix = \"{$prefix}\";
325 * Function checks the format of Oracle connect string
326 * The actual validity of the string is checked by attempting to connect
328 * Regex should be able to validate all connect string formats
329 * [//](host|tns_name)[:port][/service_name][:POOLED]
330 * http://www.orafaq.com/wiki/EZCONNECT
334 * @param string $connect_string
336 * @return bool Whether the connection string is valid.
338 public static function checkConnectStringFormat( $connect_string ) {
339 // @@codingStandardsIgnoreStart Long lines with regular expressions.
340 // @todo Very long regular expression. Make more readable?
341 $isValid = preg_match( '/^[[:alpha:]][\w\-]*(?:\.[[:alpha:]][\w\-]*){0,2}$/', $connect_string ); // TNS name
342 $isValid |
= preg_match( '/^(?:\/\/)?[\w\-\.]+(?::[\d]+)?(?:\/(?:[\w\-\.]+(?::(pooled|dedicated|shared))?)?(?:\/[\w\-\.]+)?)?$/', $connect_string ); // EZConnect
343 // @@codingStandardsIgnoreEnd
344 return (bool)$isValid;